package chan.module.project.hw.mrs.obs.v331;

import chan.module.project.comm.PropertiesUtil;
import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectRequest;
import com.obs.services.model.PutObjectResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class MRSObsV331Main {

    public static void main(String[] args) {
        String configPath = null;
        for (String param : args){
            if(param.startsWith("config=")){
                configPath = param.replace("config=","");
                break;
            }
        }
        if(configPath == null){
            System.err.println("参数值[configPath]为空！");
            return;
        }
        File configFile = new File(configPath);
        if(!configFile.exists()){
            System.err.println("配置文件[" + configFile +"] 不存在！");
            return;
        }

        PropertiesUtil propertiesUtil = new PropertiesUtil(configPath);
        Properties prop = propertiesUtil.getProp();
        String projects = prop.getOrDefault("test.projects", "").toString();
        if(projects.contains("db")){
            testConnByKerberos( prop );
        }
        if(projects.contains("obs")){
            testObs( prop );
        }
    }


    public static void testObs(Properties prop){
        ObsClient client = new ObsClient(prop.getProperty("obs.ak") ,prop.getProperty("obs.sk") ,prop.getProperty("obs.endpoint"));
        PutObjectRequest request = new PutObjectRequest();
        request.setBucketName(prop.getProperty("obs.bucketName"));
        String workDir = prop.getProperty("obs.work.dir");

        File upLoadFile = new File(prop.getProperty("obs.upLoadFile"));
        String fileName = upLoadFile.getName();
        if(StringUtils.isNotBlank(workDir)){
            fileName = workDir + "/" + fileName;
        }
        request.setObjectKey(fileName);
        request.setFile(upLoadFile);

        PutObjectResult putRes = client.putObject(request);
        System.out.println(putRes.getStatusCode());
    }

    public static void testConnByKerberos(Properties prop){
        try {
            // 加载class
            Class.forName(prop.getProperty("conn.jdbc.driver"));
            // 1. 设置Kerberos配置路径
            System.setProperty("java.security.krb5.conf", prop.getProperty("java.security.krb5.conf") );
            System.setProperty("sun.security.krb5.debug", prop.getOrDefault("sun.security.krb5.debug","false").toString() ); // 可选：开启调试

            // 2. 配置Hadoop安全上下文（用于Kerberos认证）
            Configuration conf = new Configuration();
            conf.set("hadoop.security.authentication", prop.getOrDefault("hadoop.security.authentication","Kerberos").toString() );
            UserGroupInformation.setConfiguration(conf);

            // 3. 使用keytab登录Kerberos
            String principal = prop.getProperty("kerberos.principal");
            String keytabPath = prop.getProperty("kerberos.keytabPath");
            UserGroupInformation.loginUserFromKeytab(principal, keytabPath);

            // 4. 配置JDBC连接参数
            String url = prop.getProperty("conn.url");
            /*Properties props = new Properties();
            props.setProperty("kerberosAuthentication", prop.getOrDefault("kerberos.kerberosAuthentication","true").toString() );*/ // 启用Kerberos
            //props.setProperty("user", ""); // 用户名为空（由票据提供）
            //props.setProperty("password", ""); // 密码为空

            // 5. 建立连接
            //Connection conn = DriverManager.getConnection(url, props);
            Connection conn = DriverManager.getConnection(url);
            System.out.println("连接成功！");
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
